using System;
using System.Windows;
using System.Windows.Threading;

namespace MvvmBase.ViewModel
{
    class AppDispatcherOwner : IDispatcherOwner
    {
        private readonly Dispatcher _dispatcher;

        public AppDispatcherOwner(Application app)
        {
            if(app==null)throw new ArgumentNullException("app");
            _dispatcher = app.RootVisual.Dispatcher;
        }

        public void ExectuteIfCan(Action action)
        {
            if(_dispatcher==null)return;
            if (_dispatcher.CheckAccess())
            {
                action();
            }
            else
            {
                _dispatcher.BeginInvoke(action);
            }
        }
    }
}